﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    [ObjectEvent]
    public class BattleBaseEventEvent : ObjectEvent<BattleBaseEvent>, IAwake
    {
        public void Awake()
        {
            Get().Awake();
        }
    }

    public class BattleBaseEvent : BattleEventComponent
    {
        public override BattleEventType[] EventType { get; set; } = new BattleEventType[] { BattleEventType.BattleStatusSwitch,BattleEventType.AttackSuccess, BattleEventType.DefenceSuccess,BattleEventType.BattleEnd,BattleEventType.GorgeousCal };

        public override int SortIndex { get; set; } = 4;

        public override async Task RunEvent(BattleEventType eventType, FightModel fightModel, params object[] param)
        {
            
            if (eventType == BattleEventType.AttackSuccess)
            {
                DamageInfo damageInfo = param[0] as DamageInfo;
                damageInfo.AttackCard.Owner[CardPlace.Duel].MoveCards(damageInfo.AttackCard.Owner[CardPlace.SpellChain], new List<BattleCard>() { damageInfo.AttackCard });
                if (damageInfo.DefenceCard != null)
                {
                    damageInfo.DefenceCard.CardGroup.MoveCards(damageInfo.DefenceCard.Owner[CardPlace.Cemetery], new List<BattleCard>() { damageInfo.DefenceCard });
                }
            }
            if (eventType == BattleEventType.DefenceSuccess)
            {
                DamageInfo damageInfo = param[0] as DamageInfo;
                damageInfo.AttackCard.CardGroup.MoveCards(damageInfo.AttackCard.Owner[CardPlace.Cemetery], new List<BattleCard>() { damageInfo.AttackCard });
                if (damageInfo.DefenceCard != null)
                {
                    damageInfo.DefenceCard.CardGroup.MoveCards(damageInfo.DefenceCard.Owner[CardPlace.Cemetery], new List<BattleCard>() { damageInfo.DefenceCard });
                }
            }            
            if (eventType == BattleEventType.BattleStatusSwitch)
            {
                TurnInfo turnInfo = param[0] as TurnInfo;
                if (turnInfo.BattlePlayer != fightModel.nowPlayer) return;
                switch (turnInfo.BattleStatus)
                {
                    case BattleStatus.Pick:
                        await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.TurnStart, fightModel, null);
                        if (fightModel.nowPlayer.BrokenPower > 0 && fightModel.nowPlayer.BrokenPower == fightModel.nowPlayer.MaxPower - fightModel.nowPlayer.Power)//修复灵珠
                        {
                            List<DecideInfo> decideInfo = new List<DecideInfo>()
                            {
                                new DecideInfo()
                                {
                                    CallBack=()=>DrawCardNormal(fightModel),
                                    Describe="正常抽卡",
                                },
                                new DecideInfo()
                                {
                                    CallBack=()=>{
                                        fightModel.nowPlayer.BrokenPower-=1;
                                    },
                                    Describe="消耗一次抽卡机会修复破损灵力",
                                },
                            };
                            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.ShowWindow, fightModel, WindowType.Decide, decideInfo);
                        }
                        else
                            DrawCardNormal(fightModel);
                        turnInfo.BattleStatus = BattleStatus.Change;
                        break;
                    case BattleStatus.Change:
                        turnInfo.BattleStatus = BattleStatus.Set;
                        break;
                    case BattleStatus.Set:
                        turnInfo.BattleStatus = BattleStatus.Battle;
                        break;
                    case BattleStatus.Battle:
                        turnInfo.BattleStatus = BattleStatus.End;
                        break;
                    case BattleStatus.End:
                        fightModel.ChangeNowPlayer();
                        turnInfo.BattleStatus = BattleStatus.Pick;
                        break;
                    case BattleStatus.BattleDefence:
                        FightCalComponent.Single.DontDefence(fightModel);
                        turnInfo.BattleStatus = BattleStatus.Battle;
                        break;
                }
            }
            if (eventType == BattleEventType.BattleEnd)
            {
                BattleEndInfo battleEndInfo = param[0] as BattleEndInfo;
                if (battleEndInfo.battleEndType == BattleEndType.DrawOutCard)
                {
                    await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.GorgeousCal, fightModel, null);
                }
                if (battleEndInfo.battleEndType == BattleEndType.HpLessThen0)
                {
                    fightModel.EndGame_Hp();
                }
                if (battleEndInfo.battleEndType == BattleEndType.Special)
                {
                    fightModel.EndGame_Sp(battleEndInfo);
                }
            }
            if (eventType == BattleEventType.GorgeousCal)
            {
                fightModel.EndGame_GorgeousCal();
            }
        }

        private static void DrawCardNormal(FightModel fightModel)
        {
            int drawNum = fightModel.nowPlayer.MaxPower - fightModel.nowPlayer.Power - fightModel.nowPlayer.BrokenPower;
            if (drawNum < 1) drawNum = 1;
            fightModel.nowPlayer.DrawCards(fightModel, drawNum);
            if (fightModel.nowPlayer.Power < fightModel.nowPlayer.MaxPower - fightModel.nowPlayer.BrokenPower) fightModel.nowPlayer.Power += 1;
        }
    }
}
